package com.icefish.daoImpl;

import com.icefish.bean.Good;
import com.icefish.bean.Order;
import com.icefish.dao.OrderDao;
import com.icefish.utils.DatabaseConnFactory;
import com.icefish.utils.DateTransform;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public ArrayList<Order> selectOrderByUserId(Integer userId) throws Exception {

        String sql = "select * from orders where user_id=?";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setInt(1, userId);
        ResultSet rst = psmt.executeQuery();

        ArrayList<Order> orders = new ArrayList<Order>();

        while(rst.next()){

            Order order = new Order();
            order.setOrder_id(rst.getInt("order_id"));
            order.setOrder_time(rst.getDate("order_time"));
            order.setOrder_count(rst.getInt("order_count"));
            order.setOrder_amount(rst.getInt("order_amount"));
            order.setUser_id(rst.getInt("user_id"));
            order.setUsername(rst.getString("username"));
            order.setGoods_id(rst.getInt("goods_id"));
            order.setGoods_name(rst.getString("goods_name"));
            order.setGoods_price(rst.getInt("goods_price"));
            order.setGoods_picture(rst.getString("goods_picture"));
            order.setGoods_describe(rst.getString("goods_describe"));
            order.setSays(rst.getString("says"));

            orders.add(order);

        }
        conn.close();
        psmt.close();
        rst.close();

        return orders;
    }

    @Override
    public Integer insertCartToOder(Order order) throws Exception {

        String sql="insert into orders(order_time,order_count,order_amount,user_id,username,goods_id,goods_name,goods_price,goods_picture,goods_describe) " +
                "values(?,?,?,?,?,?,?,?,?,?)";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setDate(1, DateTransform.util_To_Sql(order.getOrder_time()));
        psmt.setInt(2, order.getOrder_count());
        psmt.setInt(3,order.getOrder_amount());
        psmt.setInt(4,order.getUser_id());
        psmt.setString(5, order.getUsername());
        psmt.setInt(6,order.getGoods_id());
        psmt.setString(7,order.getGoods_name());
        psmt.setInt(8,order.getGoods_price());
        psmt.setString(9,order.getGoods_picture());
        psmt.setString(10,order.getGoods_describe());

        Integer i =psmt.executeUpdate();

        conn.close();
        psmt.close();

        return i;
    }

    @Override
    public boolean addOrderBySingleShopCart (Order order) throws Exception {

        String sql="insert into orders(order_time,order_count,order_amount,user_id,username,goods_id,goods_name,goods_price,goods_picture,goods_describe,says) " +
                "values(?,?,?,?,?,?,?,?,?,?,?)";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setDate(1, DateTransform.util_To_Sql(order.getOrder_time()));
        psmt.setInt(2, order.getOrder_count());
        psmt.setInt(3,order.getOrder_amount());
        psmt.setInt(4,order.getUser_id());
        psmt.setString(5, order.getUsername());
        psmt.setInt(6,order.getGoods_id());
        psmt.setString(7,order.getGoods_name());
        psmt.setInt(8,order.getGoods_price());
        psmt.setString(9,order.getGoods_picture());
        psmt.setString(10,order.getGoods_describe());
        psmt.setString(11,order.getSays());

        Integer i =psmt.executeUpdate();

        conn.close();
        psmt.close();

        return (i == 1);
    }


    // 修改评论数据
    @Override
    public boolean updateOrderSays (Order order) throws Exception {
        String sql="UPDATE orders SET says = ? WHERE order_id = ?";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);

        psmt.setString(1, order.getSays());
        psmt.setInt(2,order.getOrder_id());

        Integer i =psmt.executeUpdate();

        conn.close();
        psmt.close();

        return (i == 1);
    }

    @Override
    // 删除订单记录
    public boolean deleteOrderById (Integer order_id) throws Exception {
        String sql="DELETE  FROM orders  WHERE order_id = ?";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);

        psmt.setInt(1,order_id);

        Integer i =psmt.executeUpdate();

        conn.close();
        psmt.close();

        return (i == 1);
    }
}
